"""
@Time ： 2023/7/20 0009 10:07
@Auth ： zpt
@File ：CustomPackages.py
@IDE ：PyCharm
"""
import time

from selenium.webdriver.common.by import By

from Page.LoginPage import LoginPage
from selenium.webdriver.common.keys import Keys
from Lib.common_lib import LOG_INFO, LOG_ERROR


class CustomPackages(object):
    """镜像定制页面"""
    product_name = "openEuler"
    config_kernel = "net.ifnames=0 biosdevname=0 crashkernel=512M"
    repo_url = "https://repo.openeuler.org/openEuler-22.03-LTS/everything/x86_64/"
    config_version = "1.1"

    def __init__(self, driver):
        self.driver = driver

    def get_url(self, url):
        self.driver.get(url)

    def user_login(self, user_name, password):
        """
        用户登录
        """
        login = LoginPage(self.driver)
        login.user_login()
        login.login_valid(user_name, password)

    def customPackages_button(self):
        """
        定制业务包按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="left"]/div[3]/div[2]/div[1]').click()
        except Exception as e:
            message = '定制业务包按钮定位失败'
            LOG_ERROR("error: found customPackages timeout")
            print(e)
            LOG_INFO(message)

    def edit_button(self):
        """
        定制业务包页面修改按钮
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/main/div[1]/div/div/div[1]/button').click()
        except Exception as e:
            message = '修改按钮定位失败'
            LOG_ERROR("error: found edit button timeout")
            print(e)
            LOG_INFO(message)

    def edit_button_version_custom(self):
        """
        版本定制页面修改按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div/div[1]/button').click()
        except Exception as e:
            message = '版本定制页面修改按钮'
            LOG_ERROR("error: found edit button timeout")
            print(e)
            LOG_INFO(message)

    def save_button(self):
        """
        定制业务包页面保存按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div[1]/div/div/div/div[2]/button[1]').click()
        except Exception as e:
            message = '保存按钮定位失败'
            LOG_ERROR("error: found save button timeout")
            print(e)
            LOG_INFO(message)

    def save_button_version_custom(self):
        """
        版本定制iso镜像定制业务包页面保存按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div/div[1]/div/button[1]').click()
        except Exception as e:
            message = '版本定制iso镜像定制业务包页面保存按钮'
            LOG_ERROR("error: found save button timeout")
            print(e)
            LOG_INFO(message)

    def cancel_button(self):
        """
        定制业务包页面取消按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div[1]/div/div/div/div[2]/button[2]').click()
        except Exception as e:
            message = '取消按钮定位失败'
            LOG_ERROR("error: found cancel button timeout")
            print(e)
            LOG_INFO(message)

    def custom_packages_info(self, product_name, config_kernel, repo_url):
        """
        定制业务包页面产品名称、配置内核参数、添加repo源 输入框内容输入
        """
        try:
            self.name = self.driver.find_element(By.XPATH,
                                                 '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                 '2]/section/section/main/div[1]/div/div/form/div[1]/div['
                                                 '1]/div/div/div/input')
            self.kernel = self.driver.find_element(By.XPATH,
                                                   '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                   '2]/section/section/main/div[1]/div/div/form/div[1]/div['
                                                   '3]/div/div/div/textarea')
            self.url = self.driver.find_element(By.XPATH,
                                                '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                '2]/section/section/main/div[1]/div/div/form/div[3]/div/div['
                                                '1]/div/textarea')

            self.name.send_keys(product_name)
            self.name.send_keys(Keys.ENTER)

            self.kernel.send_keys(config_kernel)
            self.kernel.send_keys(Keys.ENTER)

            self.url.send_keys(repo_url)
            self.url.send_keys(Keys.ENTER)

        except Exception as e:
            message = '定制业务包模块定位失败'
            LOG_ERROR("error: found custom packages module timeout")
            print(e)
            LOG_INFO(message)

    def custom_iso_normal_packages_info(self, product_name, config_version, repo_url):
        """
        定制业务包页面产品名称、配置内核参数、添加repo源 输入框内容输入
        """
        try:
            self.name = self.driver.find_element(By.XPATH,
                                                 '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                 '2]/section/section/main/div[1]/div/div/form/div[1]/div['
                                                 '1]/div/div/div/input')
            self.version = self.driver.find_element(By.XPATH,
                                                    '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                    '2]/section/section/main/div[1]/div/div/form/div[1]/div['
                                                    '2]/div/div[1]/div/input')
            self.url = self.driver.find_element(By.XPATH,
                                                '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                '2]/section/section/main/div[1]/div/div/form/div[4]/div/div['
                                                '1]/div/textarea')

            self.name.send_keys(product_name)
            self.name.send_keys(Keys.ENTER)

            self.version.send_keys(config_version)
            self.version.send_keys(Keys.ENTER)

            self.url.send_keys(repo_url)
            self.url.send_keys(Keys.ENTER)

        except Exception as e:
            message = 'iso_normal 类型定制业务包模块定位失败'
            LOG_ERROR("error: found custom packages module timeout")
            print(e)
            LOG_INFO(message)

    def custom_docker_packages_info(self, repo_url):
        """
        docker类型定制业务包页面产品名称添加repo源
        """
        try:
            self.add_repo_url = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                   '2]/section/section/main/div[1]/div/div/form/div['
                                                                   '2]/div/div[1]/div/textarea')

            self.add_repo_url.send_keys(repo_url)
            self.add_repo_url.send_keys(Keys.ENTER)

        except Exception as e:
            message = 'docker类型定制业务包模块定位失败'
            LOG_ERROR("error: found custom packages module timeout")
            print(e)
            LOG_INFO(message)

    def custom_version_iso_packages_info(self, repo_url, name, number):
        """
        版本定制iso镜像配置软件包
        """
        try:
            self.add_repo_url = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                   '2]/section/section/main/div/div[2]/form/div['
                                                                   '1]/div/div[1]/textarea')

            self.add_repo_url.send_keys(repo_url)
            #self.add_repo_url.send_keys(Keys.ENTER)
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/main/div/div[2]/form/div['
                                               '2]/div/div/div/div/div/input').click()
            self.driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/div/div/div[1]/ul/li[1]').click()
            time.sleep(2)
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/main/div/div[2]/form/div['
                                               '3]/div[1]/div/div[1]/div/input').send_keys(name)
            time.sleep(2)
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/main/div/div[2]/form/div[3]/div[2]/div/div['
                                               '1]/div/input').send_keys(number)
        except Exception as e:
            message = '版本定制iso镜像配置软件包模块定位失败'
            LOG_ERROR("error: found custom packages module timeout")
            print(e)
            LOG_INFO(message)

    def custom_version_docker_packages_info(self, repo_url, name, number):
        """
        版本定制docker镜像配置软件包
        """
        try:
            self.add_repo_url = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                   '2]/section/section/main/div/div[2]/form/div['
                                                                   '1]/div/div[1]/textarea')

            self.add_repo_url.send_keys(repo_url)
            time.sleep(2)
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/main/div/div[2]/form/div[2]/div[1]/div/div['
                                               '1]/div/input').send_keys(name)
            time.sleep(2)
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/main/div/div[2]/form/div[2]/div[2]/div/div['
                                               '1]/div/input').send_keys(number)
        except Exception as e:
            message = '版本定制docker镜像配置软件包模块定位失败'
            LOG_ERROR("error: found custom packages module timeout")
            print(e)
            LOG_INFO(message)

    def custom_cpio_packages_info(self, product_name, repo_url):
        """
        定制业务包页面产品名称、配置内核参数、添加repo源 输入框内容输入
        """
        try:
            self.name = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                           '2]/section/section/main/div[1]/div/div/form/div['
                                                           '1]/div/div/div[1]/div/input')
            self.url = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                          '2]/section/section/main/div[1]/div/div/form/div[3]/div/div['
                                                          '1]/div/textarea')

            self.name.send_keys(product_name)
            self.name.send_keys(Keys.ENTER)

            self.url.send_keys(repo_url)
            self.url.send_keys(Keys.ENTER)

        except Exception as e:
            message = '定制业务包模块定位失败'
            LOG_ERROR("error: found custom packages module timeout")
            print(e)
            LOG_INFO(message)

    def custom_packages_info_clear(self, product_name, config_kernel, repo_url):
        """
        定制业务包页面产品名称、配置内核参数、添加repo源 输入框内容清除
        """
        self.product_name = self.driver.find_element(By.XPATH,
                                                     '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                     '2]/section/section/main/div[1]/div/div/form/div[1]/div['
                                                     '1]/div/div/div/input').clear()
        self.congfig_kernel = self.driver.find_element(By.XPATH,
                                                       '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                       '2]/section/section/main/div[1]/div/div/form/div[1]/div['
                                                       '3]/div/div/div/textarea').clear()
        self.add_repo_url = self.driver.find_element(By.XPATH,
                                                     '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                     '2]/section/section/main/div[1]/div/div/form/div[3]/div/div['
                                                     '1]/div/textarea').clear()

    def text_info_valid(self):
        """
        定制业务包页面产品名称、配置内核参数、添加repo源 输入后内容获取
        """
        product_name_info = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                               '2]/section/section/main/div[1]/div/div/form/div['
                                                               '1]/div[1]/div/div[2]')
        congfig_kernel_info = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                 '2]/section/section/main/div[1]/div/div/form/div['
                                                                 '1]/div[3]/div/div[2]')
        add_repo_url_info = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                               '2]/section/section/main/div[1]/div/div/form/div['
                                                               '3]/div/div[2]')

        return [product_name_info.text, congfig_kernel_info.text, add_repo_url_info.text]

    def add_rpms(self, pipeline_type="iso"):
        """
        定制业务包页面添加rpm包-添加按钮
        """
        try:
            if pipeline_type == "docker":
                self.driver.find_element(By.XPATH,
                                         '//*[@id="right"]/div[1]/div/div/form/div[4]/div[1]/button[1]').click()
            if pipeline_type == "qcow2":
                self.driver.find_element(By.XPATH,
                                         '//*[@id="right"]/div[1]/div/div/form/div[6]/div[1]/button[1]').click()
            else:
                self.driver.find_element(By.XPATH,
                                         '//*[@id="right"]/div[1]/div/div/form/div[5]/div[1]/button[1]').click()
        except Exception as e:
            message = '添加rpm包按钮定位失败'
            LOG_ERROR("error: found add rpms button timeout")
            print(e)
            LOG_INFO(message)

    def rpm_check_box(self, pipeline_type="iso"):
        """
        rpm包列表第一个勾选框
        """
        try:
            if pipeline_type == "qcow2":
                self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                   '2]/section/section/main/div[2]/div/div/div/div/div[2]/div[1]/div['
                                                   '2]/table/thead/tr/th[1]/div/label/span/span').click()
            else:
                self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                   '2]/section/section/main/div[4]/div/div/div/div/div[2]/div[1]/div['
                                                   '2]/table/thead/tr/th[1]/div/label/span/span').click()
        except Exception as e:
            message = '添加rpm勾选框定位失败'
            LOG_ERROR("error: found rpm list checkbox timeout")
            print(e)
            LOG_INFO(message)

    def add_rpm_confirm(self, pipeline_type="iso"):
        """
        rpm包列表确认按钮
        """
        try:
            if pipeline_type == "qcow2":
                self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                   '2]/section/section/main/div[2]/div/div/footer/span/button['
                                                   '1]').click()
            else:
                self.driver.find_element(By.XPATH, '//*[@id="right"]/div[4]/div/div/footer/span/button[1]').click()
        except Exception as e:
            message = '添加rpm包确认按钮定位失败'
            LOG_ERROR("error: found rpm list confirm timeout")
            print(e)
            LOG_INFO(message)

    def add_rpm_cancel(self):
        """
        rpm包列表取消按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div[4]/div/div/footer/span/button[2]').click()
        except Exception as e:
            message = '添加rpm包取消按钮定位失败'
            LOG_ERROR("error: found rpm list cancel timeout")
            print(e)
            LOG_INFO(message)

    def check_rpms_num(self):
        """
        定制业务包页面获取到的rpm包数量
        """
        try:
            rpms_num_text = self.driver.find_element(By.XPATH, '//*[@id="right"]/div[1]/div/div/form/div[7]/span[1]')
            return rpms_num_text.text
        except Exception as e:
            message = '添加rpm包确认按钮定位失败'
            LOG_ERROR("error: found rpm num timeout")
            print(e)
            LOG_INFO(message)

    def check_rpm_name(self):
        """
        定制业务包页面获取到的第一行rpm包名
        """
        try:
            rpm_name = self.driver.find_element(By.XPATH, '//*[@id="right"]/div[1]/div/div/form/div[6]/div[1]/div['
                                                          '3]/div/div[1]/div/table/tbody/tr[2]/td[2]/div')
            return rpm_name.text
        except Exception as e:
            message = '获取rpm包失败'
            LOG_ERROR("error: found rpm name timeout")
            print(e)
            LOG_INFO(message)

    def check_select_rpm_name(self):
        """
        定制业务包页面获取到的第一行rpm包名
        """
        try:
            select_rpm_name = self.driver.find_element(By.XPATH,
                                                       '//*[@id="right"]/div[1]/div/div/form/div[6]/div[1]/div['
                                                       '3]/div/div[1]/div/table/tbody/tr/td[2]/div')
            return select_rpm_name.text
        except Exception as e:
            message = '获取查找到的rpm包失败'
            LOG_ERROR("error: found select rpm name timeout")
            print(e)
            LOG_INFO(message)


    def check_rpm_remove(self):
        """
        定制业务包页面rpm包列表第一行移除按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div[1]/div/div/form/div[6]/div[1]/div['
                                               '3]/div/div[1]/div/table/tbody/tr[2]/td[3]/div/span').click()
        except Exception as e:
            message = '获取rpm包移除按钮失败'
            LOG_ERROR("error: found the first row remove button timeout")
            print(e)
            LOG_INFO(message)

    def check_rpm_batch_remove(self):
        """
        定制业务包页面rpm包批量移除按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div[1]/div/div/form/div[5]/div[1]/button[2]').click()
        except Exception as e:
            message = '获取rpm包批量移除按钮失败'
            LOG_ERROR("error: found rpm batch remove button timeout")
            print(e)
            LOG_INFO(message)

    def rpm_batch_check(self):
        """
        定制业务包页面rpm包批量勾选按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div[1]/div/div/form/div[6]/div[1]/div['
                                               '2]/table/thead/tr/th[1]/div/label/span/span').click()
        except Exception as e:
            message = '获取rpm包批量勾选按钮失败'
            LOG_ERROR("error: found rpm batch check timeout")
            print(e)
            LOG_INFO(message)

    def rpm_search_box(self, key):
        """
        定制业务包修改页面--rpm包搜索框
        """
        try:
            self.search_box = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                 '2]/section/section/main/div[1]/div/div/form/div[5]/div['
                                                                 '2]/div/div/input')

            self.search_box.send_keys(key)
            self.search_box.send_keys(Keys.ENTER)
        except Exception as e:
            message = '获取rpm包搜索框失败'
            LOG_ERROR("error: found rpm select button timeout")
            print(e)

    def rpm_search_box_clear(self):
        """
        定制业务包修改页面--rpm包搜索框
        """
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                           '2]/section/section/main/div[1]/div/div/form/div[5]/div['
                                           '2]/div/div/input').clear()

    def rpmlist_search_box(self, key):
        """
        rpm包列表搜索框
        """
        try:
            self.list_search_box = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                      '2]/section/section/main/div['
                                                                      '4]/div/div/div/div/div[1]/div[2]/div/div/input')

            self.list_search_box.send_keys(key)
            self.list_search_box.send_keys(Keys.ENTER)
        except Exception as e:
            message = '获取rpm包搜索框失败'
            LOG_ERROR("error: found rpmlist search box timeout")
            print(e)

    def rpmlist_one_check_box(self):
        """
        rpm包列表第一个包的复选框
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/main/div['
                                               '4]/div/div/div/div/div[2]/div[1]/div['
                                               '3]/div/div[1]/div/table/tbody/tr[1]/td['
                                               '1]/div/label/span/span').click()
        except Exception as e:
            message = '获取rpm包列表第一个包的复选框失败'
            LOG_ERROR("error: found first row checkbox timeout")
            print(e)

    def rpmlist_close(self):
        """
        rpm包列表关闭按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div[4]/div/div/header/button').click()
        except Exception as e:
            message = '获取rpm包列表关闭按钮失败'
            LOG_ERROR("error: found rpmlist close botton timeout")
            print(e)

    def rpmlist_goto_page(self):
        """
        rpm包列表前往多少页输入框
        """
        try:
            self.goto_page = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                '2]/section/section/main/div[4]/div/div/div/div/div[3]/span['
                                                                '3]/div/div/input')
            # 前往第十页
            self.goto_page.send_keys(0)
            self.goto_page.send_keys(Keys.ENTER)
        except Exception as e:
            message = '获取rpm包列表前往某页输入框失败'
            LOG_ERROR("error: found rpmlist goto page  timeout")
            print(e)

    def rpmlist_page_number(self):
        """
        rpm包列表前往第10页数字高亮属性
        """
        try:
            self.page_numberself = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                      '2]/section/section/main/div['
                                                                      '4]/div/div/div/div/div[3]/ul/li[4]')

            attribute = self.page_numberself.get_attribute("class")
            return attribute
        except Exception as e:
            message = '获取rpm包列表关闭按钮失败'
            LOG_ERROR("error: found rpmlist goto page  timeout")
            print(e)


